<php>if(is_login()){</php>
<a data-role="show_chat_frame" id="chat" href="javascript:;" class="chat" style="display: none;">
    <span class="label label-badge label-danger"
          style="display: none;margin-left: 35px;margin-top: -10px;position: absolute;"></span>
</a>
<link type="text/css" rel="stylesheet" href="{:getRootUrl()}Addons/Wukong/_static/css/wukong.css"/>
<script type="text/javascript" src="{:getRootUrl()}Addons/Wukong/_static/js/WKim-3.2.5.js"></script>
<script type="text/javascript" src="{:getRootUrl()}Addons/Wukong/_static/js/common.js"></script>
<script type="text/javascript" src="{:getRootUrl()}Addons/Wukong/_static/js/template.js"></script>

<script>
    var chat_list_url = "{:addons_url('Wukong://Wukong/chatList')}";


    var myInfo = {};
    $(function () {
        $('body').append('<div id="wk_content" style="display: none"></div>');

        $('[data-role="show_chat_frame"]').click(function () {
            $(this).find('span').hide();
            show_chat_frame()
        })

        var authInfo = '{$signature}';
        /* signature 签名*/
        WKim.auth.login(JSON.parse(authInfo)).then(function (token) {

            var friends_ids = "1";

            wk_conversion.getList(0, 20, function (res) {
                for (var i = 0; i < res.length; i++) {
                    if (res[i].isSingleChat) {
                        var other_id = res[i].peerId();
                        friends_ids += ',' + other_id;
                    }
                }

                $.post(U('ucenter/public/assignuser'), {ids: friends_ids}, function (res) {
                    var data = res;
                    for (var i = 0; i < data.length; i++) {
                        sessionStorage['user_info_' + data[i].uid] = JSON.stringify(data[i]);
                    }
                })

            })


            WKim.conversation.getTotalUnreadCount().then(function (count) {
                if (count > 0) {
                    $('#chat').find('span').text(count).show();
                }
            });


            $('.tool #chat').show();
            sessionStorage["token"] = JSON.stringify(token);
        });


        WKim.messageListener.on("msgReceive", function (res) {

            var id = get_other_id(res);
            // 设置新发消息到sessionstorage
            var session = sessionStorage["wk_message_list_" + id] || '[]';
            session = JSON.parse(session);
            session.push(res);
            sessionStorage["wk_message_list_" + id] = JSON.stringify(session);

            if ($('#wk_content').css('display') == 'none') {
                WKim.conversation.getTotalUnreadCount().then(function (count) {
                    if (count > 0) {
                        $('#chat').find('span').text(count).show();
                    }
                });
            }


            if ($('#wk_message_frame_' + id).css('display') == 'block') {
                var data = {};
                data.msg = res;
                if (res.isMe()) {
                    data.user = myInfo;
                    data.is_me = true;
                } else {
                    data.user = JSON.parse(sessionStorage['user_info_' + id]);
                    data.is_me = false;
                }
                var html = template('my_message', data);
                $('#wk_message_frame_' + id).append(html)
                go_to_frame_bottom();
                res.updateToRead();
            }
            var $chat = $('#chat_' + id);
            var check_obj = $chat.length;
            if (check_obj > 0) {
                $chat.closest('a').remove();
            }
            wk_conversion.getConv(res.baseMessage.conversationId, function (ret) {
                 //console.log(JSON.stringify(ret));
                if (ret.isSingleChat) {
                    var data = ret;
                    var other_id = ret.peerId();
                    var userInfo = get_user_info(other_id);
                    data.user = userInfo;
                    if (typeof userInfo != 'undefined') {
                        var html = template('my_chat_list', data);
                        $('#chat_body').prepend(html);
                        if ($('#wk_message_frame_' + other_id).css('display') == 'block') {
                            $('#chat_' + other_id).find('.other .un_read').html('');
                        }
                    }
                }
                bink_create_conv_click();
            })

        });


        WKim.messageListener.on("msgStatusChange", function (res) {
            $('#wk_msg_' + res.baseMessage.messageId).find('.un_read').find('a').html(res.senderMessageStatus.unReadCount + '人未读');
        });


    })
</script>

<php>}</php>